//
//  UserViewController.swift
//  miaoyin
//
//  Created by David on 2017/2/14.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class UserViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var customBar: UINavigationBar!
    var navItem: UINavigationItem!
    
    var tableView: UITableView!
    
    var avatarImgBtn: UIButton!
    var avatarImgBtnInScroll: UIButton!
    
    var loginBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.setNavigationBarHidden(true, animated: false)
        view.backgroundColor = UIColor.groupTableViewBackground
        
        
        tableView = UITableView(frame: CGRect(x: 0, y: 44, width: view.bounds.width, height: view.bounds.height - 44), style: .grouped)
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        
        customBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 64))
        
        navItem = UINavigationItem(title: "我的")
        
        navItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: nil, action: nil)
        
        customBar.pushItem(navItem, animated: false)
        
        view.addSubview(customBar)
        
        avatarImgBtn = UIButton(frame: CGRect(x: 16 - 50, y: 24 - 50, width: 100, height: 100))
        avatarImgBtn.layer.anchorPoint = CGPoint(x: 0, y: 0)
        avatarImgBtn.layer.cornerRadius = 50
        avatarImgBtn.layer.borderColor = UIColor.lightGray.cgColor
        avatarImgBtn.layer.borderWidth = 2
        avatarImgBtn.backgroundColor = themeColor1
        view.addSubview(avatarImgBtn)
        
        loginBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        loginBtn.center = view.center
        loginBtn.setTitle("登陆", for: .normal)
        loginBtn.setTitleColor(UIColor.white, for: .normal)
        loginBtn.backgroundColor = themeColor1
        loginBtn.layer.cornerRadius = 50
        view.addSubview(loginBtn)
//        loginBtn.alpha = 0
        
        if checkLoginState(){
            
        }
        
    }
    
    func checkLoginState() -> Bool{
        tableView.alpha = isLogin() ? 1 : 0
        avatarImgBtn.alpha = isLogin() ? 1 : 0
        customBar.alpha = isLogin() ? 1 : 0
        loginBtn.alpha = isLogin() ? 0 : 1
        return isLogin()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0.001
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0{
            switch indexPath.row {
            case 0:
                return 128
            case 1:
                return 128
            default:
                return 44
            }
        }
        
        return 44
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "item")
        cell.selectionStyle = .none
        if indexPath.section == 0{
            switch indexPath.row {
            case 0:
                
                break
            default:
                break
            }
        }
        
        return cell
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let posY = scrollView.contentOffset.y + 20
//        print(posY)
        if posY < 0{
//            avatarImgBtn.center.x = 16 + avatarImgBtn.frame.width/2
            avatarImgBtn.center.y = 24 - posY
            avatarImgBtn.transform = CGAffineTransform(scaleX: 1, y: 1)
        }else if posY == 0{
//            avatarImgBtn.center.x = 16 + avatarImgBtn.frame.width/2
            avatarImgBtn.center.y = 24
            avatarImgBtn.transform = CGAffineTransform(scaleX: 1, y: 1)
        }else if posY < 42 {
            avatarImgBtn.center.y = 24
            let rate = (64 - posY)/64
//            avatarImgBtn.center.x = 16 + avatarImgBtn.frame.width*(rate)/2
//            avatarImgBtn.center.y = avatarImgBtn.frame.width*(rate)/2 + 36
            
            avatarImgBtn.transform = CGAffineTransform(scaleX: rate, y: rate)
        }else{
            avatarImgBtn.center.y = 24
            let rate: CGFloat = 22.0/64.0
//            avatarImgBtn.center.x = 16 + avatarImgBtn.frame.width*(rate)/2
//            avatarImgBtn.center.y = avatarImgBtn.frame.width*(rate)/2 + 36
            avatarImgBtn.transform = CGAffineTransform(scaleX: rate, y: rate)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
